package com.jwsoft.manager.core.util;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.DesensitizedUtil;
import com.jwsoft.manager.common.vo.eduClassStudent.EduClassStudentVO;
import com.jwsoft.manager.common.vo.eduStudentEnroll.EduStudentEnrollVO;
import org.apache.commons.lang3.StringUtils;

/***
 * 脱敏工具类
 * */
public class EduDesensitizeUtil {

    //报名信息脱敏
    public static void desensitizeStudentEnroll(EduStudentEnrollVO studentEnroll) {
        studentEnroll.setFullName(desensitizeFullName(studentEnroll.getFullName()));
        studentEnroll.setIdcard(desensitizeIdcard(studentEnroll.getIdcard()));
        studentEnroll.setFatherName(desensitizeFullName(studentEnroll.getFatherName()));
        studentEnroll.setFatherIdcard(desensitizeIdcard(studentEnroll.getFatherIdcard()));
        studentEnroll.setFatherCellphone(desensitizeCellphone(studentEnroll.getFatherCellphone()));
        studentEnroll.setMotherName(desensitizeFullName(studentEnroll.getMotherName()));
        studentEnroll.setMotherIdcard(desensitizeIdcard(studentEnroll.getMotherIdcard()));
        studentEnroll.setMotherCellphone(desensitizeCellphone(studentEnroll.getMotherCellphone()));
        studentEnroll.setAddress(desensitizeAddress(studentEnroll.getAddress()));
        studentEnroll.setHouseholdPlace(desensitizeAddress(studentEnroll.getHouseholdPlace()));
        studentEnroll.setHouseholdAddress(desensitizeAddress(studentEnroll.getHouseholdAddress()));
        studentEnroll.setHouseAdreess(desensitizeAddress(studentEnroll.getHouseAdreess()));
        studentEnroll.setRealProperty(desensitizeRealProperty(studentEnroll.getRealProperty()));
        studentEnroll.setRealPropertyNum(desensitizeOther(studentEnroll.getRealPropertyNum()));
    }

    //分班名单信息脱敏
    public static void desensitizeStudentClass(EduClassStudentVO studentEnroll) {
        studentEnroll.setFullName(desensitizeFullName(studentEnroll.getFullName()));
        studentEnroll.setIdcard(desensitizeIdcard(studentEnroll.getIdcard()));
        studentEnroll.setFatherName(desensitizeFullName(studentEnroll.getFatherName()));
        studentEnroll.setFatherIdcard(desensitizeIdcard(studentEnroll.getFatherIdcard()));
        studentEnroll.setFatherCellphone(desensitizeCellphone(studentEnroll.getFatherCellphone()));
        studentEnroll.setMotherName(desensitizeFullName(studentEnroll.getMotherName()));
        studentEnroll.setMotherIdcard(desensitizeIdcard(studentEnroll.getMotherIdcard()));
        studentEnroll.setMotherCellphone(desensitizeCellphone(studentEnroll.getMotherCellphone()));
        studentEnroll.setAddress(desensitizeAddress(studentEnroll.getAddress()));
        studentEnroll.setHouseholdAddress(desensitizeAddress(studentEnroll.getHouseholdAddress()));
        studentEnroll.setHouseAddress(desensitizeAddress(studentEnroll.getHouseAddress()));
    }
    /**
     * 姓名脱敏
     **/
    public static String desensitizeFullName(String fullName) {
        if (StringUtils.isBlank(fullName)) {
            return fullName;
        }
        if (Validator.isChinese(fullName)) {
            return "*" + fullName.substring(1);
        } else {
            if (fullName.length() > 5) {
                return "***" + fullName.substring(3);
            } else {
                return "*" + fullName.substring(1);
            }

        }
    }

    /**
     * 姓名脱敏 只留姓
     **/
    public static String desensitizeFullNameX(String fullName) {
        if (StringUtils.isBlank(fullName)) {
            return fullName;
        }
        if (Validator.isChinese(fullName)) {
            String str = "";
            for (int i = 1; i < fullName.length(); i++) {
                str = str + "*";
            }
            return fullName.substring(0, 1) + str;
        } else {
            if (fullName.length() > 5) {
                String str = "";
                for (int i = 3; i < fullName.length(); i++) {
                    str = str + "*";
                }
                return fullName.substring(0, 3) + str;
            } else {
                String str = "";
                for (int i = 1; i < fullName.length(); i++) {
                    str = str + "*";
                }
                return fullName.substring(0, 1) + str;
            }

        }
    }
    /**
     * 证件号码脱敏
     **/
    public static String desensitizeIdcard(String idcard) {
        if (StringUtils.isBlank(idcard)) {
            return idcard;
        }
        int length = idcard.length();
        String str = "";
        for (int i = 2; i < length; i++) {
            str = str + "*";
        }
        return StringUtils.left(idcard, 1) + str + StringUtils.right(idcard, 1);
    }

    /**
     * 电话号码脱敏
     **/
    public static String desensitizeCellphone(String cellphone) {
        if (StringUtils.isBlank(cellphone)) {
            return cellphone;
        }
        if (cellphone.length() >= 11) {
            return cellphone.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        } else {
            int length = cellphone.length();
            String str = "";
            for (int i = 5; i < length; i++) {
                str = str + "*";
            }
            return cellphone.replaceAll("(\\w{2})\\w*(\\w{3})", "$1" + str + "$2");
        }

    }

    /**
     * 地址脱敏
     **/
    public static String desensitizeAddress(String address) {
        if (StringUtils.isBlank(address)) {
            return address;
        }
        if (address.length() > 15) {
            return DesensitizedUtil.address(address, 8);
        } else {
            return DesensitizedUtil.address(address, 5);
        }
    }

    /**
     * 房产证号脱敏
     **/
    public static String desensitizeRealProperty(String realProperty) {
        if (StringUtils.isBlank(realProperty)) {
            return realProperty;
        }
        String hiderealProperty = "";
        for (int i = 0; i < realProperty.length(); i++) {
            if (Character.isDigit(realProperty.charAt(i))) {
                hiderealProperty = hiderealProperty + "*";
            } else {
                hiderealProperty = hiderealProperty + realProperty.substring(i, i + 1);
            }
        }
        return hiderealProperty;
    }

    /**
     * 其他脱敏
     **/
    public static String desensitizeOther(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        if (str.length() >= 6) {
            return str.replaceAll("(\\w{2})\\w*(\\w{2})", "$1****$2");
        } else {
            return str.replaceAll("(\\w{1})\\w*(\\w{1})", "$1****$2");
        }
    }
}
